package com.jdcjk.project.account.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jdcjk.R;
import com.jdcjk.project.account.model.entity.AddressInfo;
import com.jdcjk.util.dtr.zxing.AddressInfoUtis;

import java.util.ArrayList;


/**
 * Created by dong on 2016/3/4.
 * 地址弹出面板
 */
public class AddressPopupWindow extends PopupWindow {
    private Activity mContext;
    private AddressInfoUtis.AddressType mAddressType;
    private ArrayList<AddressInfo> mAddressInfos;
    private View mAddressView;
    private TextView title;
    private ListView data;
    private Button cancel;
    private int width;
    private int height;

    public AddressPopupWindow(final Activity context, AddressInfoUtis.AddressType addressType, AdapterView.OnItemClickListener itemOnClick, ArrayList<AddressInfo> addressInfos) {
        super(context);
        this.mContext = context;
        this.mAddressType = addressType;
        this.mAddressInfos = addressInfos;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//取得xml里定义的view
        mAddressView = inflater.inflate(R.layout.popupwindow_address_panel, null);
        title = (TextView) mAddressView.findViewById(R.id.qz_dong_id_popuwindow_address_panel_title);
        cancel = (Button) mAddressView.findViewById(R.id.qz_dong_id_popuwindow_address_panel_cancel);
        cancel.setOnClickListener(onClickListener);
        data = (ListView) mAddressView.findViewById(R.id.qz_dong_id_popuwindow_address_panel_data);

        if (mAddressType == AddressInfoUtis.AddressType.PROVINCE) { //[PROVINCE模式]省份
            title.setText("省份");
        } else if (mAddressType == AddressInfoUtis.AddressType.CITY) { //[CITY模式]城市
            title.setText("城市");
        } else if (mAddressType == AddressInfoUtis.AddressType.AREA) { //[AREA模式]地区
            title.setText("地区");
        }
        initListView(mAddressInfos);
        data.setOnItemClickListener(itemOnClick);

        /**
         * PopupWindow相关属性
         */
        //设置SelectPicPopupWindow的view
        this.setContentView(mAddressView);
        //获取屏幕宽、高
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.4
        this.height = (int) (d.heightPixels * 0.8); // 高度设置为屏幕的1.0
        //设置SelectPicPopupWindow弹出窗口的宽
        this.setWidth(this.width);
        //设置SelectPicPopupWindow弹出窗口的高
        this.setHeight(this.height);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setTouchable(true);
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopuwindowAddressAnimation);
        //实例化一个ColorDrawable颜色为透明
        ColorDrawable dw = new ColorDrawable(00000);
        //设置SelectPicPopupWindow弹出框需要软键盘
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
        //设置弹出的软键盘大少和弹出框一样，覆盖尺寸,不会挡着popupwindow
        //[SOFT_INPUT_ADJUST_PAN]:覆盖在PopuWindow上、  [SOFT_INPUT_ADJUST_RESIZE]:总在PopuWindow下，不会挡着
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // 当菜单出现时菜单以外的区域是否接受点击事件
        this.setOutsideTouchable(true);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    //初始化ListView
    public void initListView(ArrayList<AddressInfo> addressInfos) {
        AddressAdapter addressAdapter = new AddressAdapter(mContext, addressInfos);
        data.setAdapter(addressAdapter);
        addressAdapter.notifyDataSetChanged();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public class AddressAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<AddressInfo> mAddressInfos;

        public AddressAdapter(Context context, ArrayList<AddressInfo> addressInfos) {
            this.mContext = context;
            this.mAddressInfos = addressInfos;
        }

        @Override
        public int getCount() {
            return mAddressInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return mAddressInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_address, null);
                viewHolder.title = (TextView) convertView.findViewById(R.id.qz_dong_id_address_item_textview);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.title.setText(mAddressInfos.get(position).getName());

            return convertView;
        }
    }

    final static class ViewHolder {
        TextView title;
    }
}
